package com.wsd.web;

import com.alibaba.fastjson.JSONObject;
import com.cif.domain.Customer;
import com.cif.service.CustomerService;
import com.wsd.domain.OverdueLoan;
import com.wsd.service.SOverdueLoanService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Classname SOverdueLoanController
 * @Description TODO
 * @Date 2020/7/20 11:01
 * @Created by niuxiaojuan
 */
@RestController
@RequestMapping("/manage")
public class SOverdueLoanController extends BaseController{
    @Autowired
    SOverdueLoanService sOverdueLoanService;

    @Reference(version = "1.0.0", url = "${dubbo.service.cifurl}")
    private CustomerService customerService;

    @RequestMapping("/getSOverLoanCount")
    public Map SOverdueLoanCount()
    {
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            BigDecimal loanMoney = sOverdueLoanService.sumSOverdueLoan();
            BigDecimal repayLoanMoney = sOverdueLoanService.sumRepayLoanMoney();
            BigDecimal interest = sOverdueLoanService.findSOverInterest();//正常贷款应收总利息
            BigDecimal repayInterest = sOverdueLoanService.sumRepayInterest();
            BigDecimal overdueMoney = sOverdueLoanService.sumSOverdueMoney();
            BigDecimal overdueInterest = sOverdueLoanService.sumSOverdueInterest();

            map.put("loanMoney",loanMoney);
            map.put("repayLoanMoney",repayLoanMoney);
            map.put("interest",interest);
            map.put("repayInterest",repayInterest);
            map.put("overdueMoney",overdueMoney);
            map.put("overdueInterest",overdueInterest);

            map.put("success","true");
            map.put("resultCode", "000");
            map.put("resultMsg", "OK");
        }catch(Exception e)
        {
            map.put("success","false");
            map.put("resultCode", "LOANCOUNT-ERROR");
            map.put("resultMsg","返回短期逾期贷款统计信息出错");
        }
        return map;
    }

    @RequestMapping("/getSOverLoanCountList")
    public Map getSOverLoanCountList(@RequestParam("pagenum") int pagenum, @RequestParam("num") int num)
    {
        Map<String, Object> map = new HashMap<String, Object>();
        try{
        List<OverdueLoan> list=sOverdueLoanService.findSOverLoanForm(pagenum,num);
        for(OverdueLoan l:list)
        {
            com.cif.domain.Customer c = JSONObject.parseObject(JSONObject.toJSONString(changeResult(customerService.getCustomerById(l.getUserId().toString())).getData()), Customer.class);
            l.setUserName(c.getUserName());
            l.setIdNumber(c.getIdNumber());
        }
            map.put("success","true");
            map.put("resultCode", "000");
            map.put("resultMsg", "OK");
            map.put("recordCount",sOverdueLoanService.countSOverLoanForm());
            map.put("list", list);
    }catch(Exception e)
    {
        map.put("success","false");
        map.put("resultCode", "GETLOANLIST-ERROR");
        map.put("resultMsg","返回贷款列表信息错误");
    }
        return map;
    }
}
